package com.yk.faceapplication.http;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.yk.faceapplication.MyApplication;
import com.yk.faceapplication.util.LogDebugger;


public class VolleyUtil extends Request<JSONObject> {

	private Map<String, String> mMap;
	private Listener<JSONObject> mListener;
	private byte[] requestStringBytes;
	
	public VolleyUtil(String url, Listener<JSONObject> listener,
			ErrorListener errorListener, Map<String, String> map) {
		super(Request.Method.POST, url, errorListener);
		mListener = listener;
		mMap = map;
		String jsonObject = com.alibaba.fastjson.JSONObject.toJSONString(mMap);
		LogDebugger.info(jsonObject);
		requestStringBytes = jsonObject.getBytes();
	}

//	@Override
//	protected Map<String, String> getParams() throws AuthFailureError {
////		if(MyApplication.Cookie != null ){
////			mMap.put("JSESSIONID", MyApplication.Cookie);
////		}
//		return mMap;
//	}

	@Override
	public String getBodyContentType() {
		return String.format("application/x-www-form-urlencoded; charset=%s", "utf-8");
	}

	@Override
	protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
		//TODO  COOKIE设置
		
//		LogDebugger.info(response.headers+"");
//		LogDebugger.info(response.headers.get("Set-Cookie"));
//		MyApplication.Cookie = response.headers.get("Set-Cookie").split(";")[0].split("=")[1];

//		if(response.headers.containsKey("Set-Cookie"))
//		MyApplication.Cookie = response.headers.get("Set-Cookie");


		try {
			String jsonString = new String(response.data,
					HttpHeaderParser.parseCharset(response.headers));
			if(jsonString.equals("true")){
				jsonString = "{result:\"true\"}";
			}else if(jsonString.equals("false")){
				jsonString = "{result:\"false\"}";
			}
			return Response.success(new JSONObject(jsonString),
					HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JSONException je) {
			return Response.error(new ParseError(je));
		}
	}
            
	@Override
	protected void deliverResponse(JSONObject response) {
		mListener.onResponse(response);
	}
	
	private Map<String, String> mHeaders = new HashMap<String, String>(1);

	public void setCookie(String cookie) {
		mHeaders.put("Cookie", cookie);
	}

	public void setGzip() {
		mHeaders.put("Accept-Encoding", "gzip,deflate");// gzip请求
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
//		if(MyApplication.Cookie != null) mHeaders.put("Cookie", MyApplication.Cookie);// gzip请求
//		return mHeaders;
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Charset", "UTF-8");
		headers.put("Content-Type", "application/x-javascript");
		headers.put("Accept-Encoding", "gzip,deflate");
		return headers;
	}

	@Override
	public byte[] getBody() throws AuthFailureError {
		LogDebugger.info("getBody:"+requestStringBytes);
		return requestStringBytes;
	}


}
